<?php
    $template = 'edit-vin';
    
    if(isset($_GET['category_id']) && $_GET['category_id']>0){
        $category = new modele_category($_GET['category_id']);
        if($category->getNombrePublications()>0){
            $listeProduits = $category->getPublications();
            
        }
    }
?>
<?php if(navigation::getAdminAction() === 'create' || navigation::getAdminAction() === 'update' ): ?>
<?php
if(navigation::getAdminModeleId()>0){
    $publication = new modele_publication(navigation::getAdminModeleId());
    $data = $publication->getDatas();
    $data['permalien'] = $publication->getPermalien()->getCleanedLien(false);
    $action = 'update';
}else{
    $action = 'create';
    $data = array();
}
?>
<h1><a class="pwe-ui-btn" title="Retour" href="?template=<?php echo $template; ?>&category_id=<?php echo $category->getId(); ?>"><span class="ui-icon ui-icon-arrowthick-1-w"></span></a> Modifier/ajouter un produit dans la carte "<?php echo $category->getTitle(); ?>"</h1>
<hr style="margin: 5px 0 15px 0;">
<form style="margin: 12px 0;" method="post" enctype="multipart/form-data" action="?template=<?php echo $template; ?>&category_id=<?php echo $category->getId(); ?>&action=<?php echo $action; ?>&modele=publication&do=1&modele_id=<?php echo (isset($data['id']))?$data['id']:''; ?>">
    <div class="columns ten">
        <p style="margin: 10px 0;"><input type="text" name="modele_title" value="<?php echo (isset($data['title']))?$data['title']:''; ?>" class="titreModele"  onblur="javascript:generatePermalien($(this).val());"/></p>
        <p style="margin: 10px 0;"><?php echo PWE::getBaseURL(); ?><input id="permalien" type="text" name="modele_lien" value="<?php echo (isset($data['permalien']))?$data['permalien']:''; ?>" style="font-size: 1.2em;" onblur="javascript:validPermalien($(this).val());"/>.html</p>
        <p style="margin: 10px 0;">Prix : <input type="text" name="modele_custom_vars[prix]" value="<?php echo (isset($data['custom_vars'])&&isset($data['custom_vars']['prix']))?$data['custom_vars']['prix']:''; ?>"/> &euro;</p>
        <textarea class="editor" name="modele_content"><?php echo (isset($data['content']))?$data['content']:''; ?></textarea><br/>
        <p style="margin: 10px 0;">Titre SEO : <input type="text" maxlength="255" name="modele_titre_seo" value="<?php echo (isset($data['titre_seo']))?$data['titre_seo']:''; ?>" style="width: 450px;"/></p>
        <p style="margin: 10px 0;">Description SEO : <textarea  maxlength="512" name="modele_description_seo" style="height: 100px;"><?php echo (isset($data['description_seo']))?$data['description_seo']:''; ?></textarea></p>
        <div align="right">
            <input type="submit" value="Enregistrer" class="pwe-ui-btn"/> 
            <?php if(isset($data['id']) && $data['id']>0): ?><a href="<?php echo $data['url']; ?>" target="_blank" title="voir" class="pwe-ui-btn">voir</a><?php endif; ?>
        </div>
    </div>
    <div class="columns five">
        <h3>Statut</h3>
        <select name="modele_statut_id">
            <option value="1" <?php echo(isset($data['statut_id']) && $data['statut_id']=='1')?'selected="selected"':''; ?>>Brouillon</option>
            <option value="2" <?php echo(isset($data['statut_id']) && $data['statut_id']=='2')?'selected="selected"':''; ?>>Publié</option>
        </select><br/><br/>
        <?php include('../admin/include/inc/mediaManager.inc.php'); ?>
        <?php include('../admin/include/inc/tagManager.inc.php'); ?>
        <input type="hidden" name="modele_date" value="<?php echo date('Y-m-d H:i:s'); ?>" />
        <input type="hidden" name="modele_template" value="fullwidth-content" />
        <input type="hidden" name="modele_category_id" value="<?php echo $category->getId(); ?>" />
        <input type="hidden" name="modele_id" value="<?php echo (isset($data['id']))?$data['id']:''; ?>" />
    </div>
</form>
<?php else: ?>
<?php
    if(isset($_POST) && isset($_POST['save_order'])){
        foreach($_POST as $var=>$value){
            if(substr($var,0,6)==='order_'){
                list($s, $idp) = explode('_',$var,2);
                $publication = new modele_publication($idp);
                $publication->setDatas(array('_order_idx'=>$value,
                    '_title'=>$publication->getTitle(),
                    '_content'=>$publication->getContent(),
                    '_titre_seo'=>$publication->getTitreSEO(),
                    '_description_seo'=>$publication->getDescriptionSEO(),
                ));
                $publication->update();
            }
        }
    }
?>
<h1><a class="pwe-ui-btn" title="Retour" href="?template=cartes"><span class="ui-icon ui-icon-arrowthick-1-w"></span></a> Liste des produits pour la carte des <?php echo $category->getTitle(); ?></h1>
<hr style="margin: 5px 0 25px 0;">
<form method="post">
    <div align="right"><a class="pwe-ui-btn" href="?template=<?php echo $template; ?>&modele=publication&action=create&category_id=<?php echo $category->getId(); ?>">Nouveau</a><br/><br/></div>
    <table class="liste ui-widget">
        <thead class="ui-widget-header">
            <tr>
                <th>Titre</th>
                <th>Prix</th>
                <th>Auteur</th>
                <th>Dernière modification</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody class="liste-items-menu">
            <?php foreach($category->getPublications() as $i=>$publication): ?>
            <tr class="ui-widget-content" style="cursor: move;">
                <td>
                    <span><?php echo $publication->getTitle(); ?></span><br/>
                    <input type="hidden" class="orderValue" name="order_<?php echo $publication->getId(); ?>" value="<?php echo $i+1; ?>" />
                </td>
                <td><?php echo ($publication->getCustomVar('prix')!==null)?$publication->getCustomVar('prix'):0; ?> &euro;</td>
                <td>
                    <b><?php echo $publication->getAuteur(); ?></b><br/>
                    (<?php echo $publication->getAuteurLevel(); ?>)
                </td>
                <td>
                    <?php echo $publication->getDate(); ?><br/>
                    <b><?php echo $publication->getStatut(); ?></b>
                </td>
                <td>
                    <a class="pwe-ui-btn" title="Modifier" href="?template=<?php echo $template; ?>&category_id=<?php echo $category->getId(); ?>&modele=publication&action=update&modele_id=<?php echo $publication->getId(); ?>"><span class="ui-icon ui-icon-pencil"></span></a>
                    <a href="?template=<?php echo $template; ?>&category_id=<?php echo $category->getId(); ?>&modele=publication&action=delete&do=1&modele_id=<?php echo $publication->getId(); ?>" class="confirmAction pwe-ui-btn" title="Supprimer '<?php echo $publication->getTitle(); ?>'"><span class="ui-icon ui-icon-trash"></span></a>
                    
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot></tfoot>
    </table>
    <div align="right"><br/>(modifier l'ordre en déplassant les lignes par glisser/déposer) <input class="pwe-ui-btn" type="submit" name="save_order" value="enregistrer l'ordre" /></div>
</form>
<?php endif; ?>